package top.lishuoboy.hutool.core.date;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * 计时器
 */
public class TimeIntervalTest {

    @Test
    public void test1() throws InterruptedException {
//        TimeInterval timer = DateUtil.timer(true);
        TimeInterval timer = DateUtil.timer();
        timer.start();

        TimeUnit.SECONDS.sleep(1);
        //花费毫秒数
        System.out.println(timer.interval());
        TimeUnit.SECONDS.sleep(2);
        //花费毫秒数
        System.out.println(timer.interval());
        //花费秒数
        System.out.println(timer.intervalSecond());

        //返回花费时间，并重置开始时间
        timer.intervalRestart();
        TimeUnit.SECONDS.sleep(2);
        //花费毫秒数
        System.out.println(timer.interval());
    }

    /**
     * 分组
     *
     * @throws InterruptedException
     */
    @Test
    public void test2() throws InterruptedException {
//        TimeInterval timer = DateUtil.timer(true);
        TimeInterval timer = DateUtil.timer();

        TimeUnit.SECONDS.sleep(1);
        timer.start("1");
        TimeUnit.SECONDS.sleep(2);
        timer.start("2");
        TimeUnit.SECONDS.sleep(3);
        //花费毫秒数
        System.out.println(timer.interval());
        System.out.println(timer.interval("1"));
        System.out.println(timer.interval("2"));
    }
}
